package com.blog.controller;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.blog.entity.User;
import com.blog.service.UserService;
import com.blog.service.Impl.UserServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;


@WebServlet("/update.do")
public class updateUserServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String userName = request.getParameter("username") == null ? "" : request.getParameter("username");
		String password = request.getParameter("password") == null ? "" : request.getParameter("password");
		String userPhone = request.getParameter("userPhone") == null ? "" : request.getParameter("userPhone");
		String userSex = request.getParameter("userSex") == null ? "" : request.getParameter("userSex");
		String userEmail= request.getParameter("userEmail") == null ? "" : request.getParameter("userEmail");
		String individualResume= request.getParameter("individualResume") == null ? "" : request.getParameter("individualResume");
		String userBirth = request.getParameter("userBirth");
		
		  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
		   Date date = null; 
		   try { 
		    date = format.parse(userBirth); 
			System.out.println(date);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");    //格式化规则
	        Date date1 = date;         //获得你要处理的时间 Date型
	        String strDate= sdf.format(date); //格式化成yyyy-MM-dd格式的时间字符串
	        Date newDate =sdf.parse(strDate);
	        System.out.println(newDate);
			UserService us= new  UserServiceImpl();
			User user=new User();
			user.setUserName(userName);
			user.setUserPass(password);
			user.setIndividualResume(individualResume);
			user.setUserBirth(date);
			user.setUserPhone(userPhone);
			user.setUserEmail(userEmail);
			user.setUserSex(userSex);
			response.setContentType("application/json;charset=utf-8");
			new ObjectMapper().writeValue(response.getWriter(),us.updateById(user));
		   } catch (ParseException e) { 
			    e.printStackTrace(); 
			   }
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
